' requesters.bmx

Strict

Import "host.bmx"

Type TCmdLineRequester Extends TRequester
	Field	label:TGadget,textfield:TGadget

	Method OnEvent()
		Local	id,data
		id=EventID()
		data=EventData()
		Select EventSource()
			Case window
				If EventID()=EVENT_WINDOWCLOSE
					Hide
				EndIf
			Case ok
				host.SetCommandLine TextFieldText(textfield)
				Hide				
			Case cancel
				SetGadgetText textfield,host.GetCommandLine()
				Hide
		End Select
	End Method
	
	Method Show()
		SetGadgetText textfield,host.GetCommandLine()
		Super.Show()
		ActivateGadget textfield
	End Method

	Function Create:TCmdLineRequester(host:THost)
		Local	cmd:TCmdLineRequester
		cmd=New TCmdLineRequester
		cmd.initrequester(host,"Program Command Line")
		cmd.label=CreateLabel("Program command line:",8,8,260,20,cmd.window)
		cmd.textfield=CreateTextField(8,32,260,20,cmd.window)
		Return cmd
	End Function
End Type

Type TGotoRequester Extends TRequester
	Field	linenumber:TGadget

	Method Show()
		Super.Show()
		ActivateGadget linenumber
	End Method

	Method OnEvent()
		Local	line,data
		Select EventSource()
			Case window
				If EventID()=EVENT_WINDOWCLOSE
					Hide
				EndIf
			Case ok
				line=Int(TextFieldText(linenumber))
				Hide
				host.activepanel.Invoke "GOTO",String(line)
			Case cancel
				Hide
		End Select
	End Method

	Function Create:TGotoRequester(host:THost)
		Local	seek:TGotoRequester
		seek=New TGotoRequester
		seek.initrequester(host,"Goto Line",220,128)
		CreateLabel("Line Number:",20,24,74,20,seek.window)
		seek.linenumber=CreateTextField(94,20,100,20,seek.window)
		Return seek
	End Function
End Type

Type TNewRequester Extends TRequester
	Field	typecombo:TGadget
	Field	index
	Field	ext$
	Global	shown'tempcount

	Method Show()
		Local	plugin:TPlugin
		Local 	text$
		ClearGadgetItems typecombo
		For plugin=EachIn host.pluginlist
			If plugin.filters
				text$=plugin.shortname+" ("+plugin.filters[0]+")"
				AddGadgetItem typecombo,text,0,-1,"",plugin.filters[0]
			EndIf
		Next	
		If index<>-1
			SelectGadgetItem typecombo,index
			ext=String(GadgetItemExtra(typecombo,index))
		EndIf
		Super.Show()
		ActivateGadget typecombo
	End Method

	Method OnEvent()
		Local	line,data
		Select EventSource()
			Case window
				If EventID()=EVENT_WINDOWCLOSE
					Hide
				EndIf
			Case typecombo
				index=EventData()
				If index<>-1 ext=String(EventExtra())
			Case ok
				Hide
				If ext
					Local path$=host.CreateTempFile(ext)	
					If path host.OpenSource path
				EndIf
			Case cancel
				Hide
		End Select
	End Method

	Function Create:TNewRequester(host:THost)
		Local	req:TNewRequester
		req=New TNewRequester
		req.initrequester(host,"New File",320,128)
		CreateLabel("File Type:",20,24,74,20,req.window)
		req.typecombo=CreateComboBox(94,20,200,224,req.window)
		Return req
	End Function
End Type

Type TProgressRequester Extends TRequester
	Field	message$,value
	Field	showing
	Field	label:TGadget
	Field	progbar:TGadget
	
	Method Show()	'returns false if cancelled
		showing=True
		Super.Show
	End Method
	
	Method Hide()
		showing=False
		Super.Hide()
	End Method
	
	Method Open(title$)
		SetGadgetText window,title
		Show
	End Method
	
	Method Update(msg$,val)
		If msg$<>message
			message=msg
			If label FreeGadget label
			label=CreateLabel(message,8,8,260,20,window)
		EndIf
		If showing And (val&$fc)<>(value&$fc)	'only update every 4 percent
			UpdateProgBar( progbar,val/100.0 )
			PollSystem
		EndIf
		value=val
	End Method
	
	Function Create:TProgressRequester(host:THost)
		Local	progress:TProgressRequester
		progress=New TProgressRequester
		progress.initrequester(host,"ProgressRequester",280,128,1)	'1 for cancel button
		progress.progbar=CreateProgBar( 8,32,260,20,progress.window )
		Return progress
	End Function
End Type

Type TFindRequester Extends TRequester
	Field	findterm:TGadget
	
	Method ShowFind(term$="")
		If term 'And GadgetText(findterm)=""
			SetGadgetText(findterm,term)
		EndIf
		Super.Show()
		ActivateGadget findterm
	End Method

	Method OnEvent()
		Local	find$,data		
		Select EventSource()
			Case window
				If EventID()=EVENT_WINDOWCLOSE
					Hide
				EndIf				
			Case ok
				find=TextFieldText(findterm)
				Hide
				host.activepanel.Invoke "FINDNEXT",find
			Case cancel
				Hide
		End Select
	End Method

	Function Create:TFindRequester(host:THost)
		Local	seek:TFindRequester
		seek=New TFindRequester
		seek.initrequester(host,"Find",280,128)
		CreateLabel("Find What",6,18,80,24,seek.window)
		seek.findterm=CreateTextField(88,14,180,20,seek.window)
		Return seek
	End Function
End Type

Type TReplaceRequester Extends TRequester
	Field	findterm:TGadget
	Field	replaceterm:TGadget
	Field	findnext:TGadget,replaceit:TGadget,replaceall:TGadget
	
	Method Show()
		Super.Show()
		ActivateGadget findterm
	End Method

	Method OnEvent()
		Local	find$,Replace$
		Select EventSource()
			Case window
				If EventID()=EVENT_WINDOWCLOSE
					Hide
				EndIf
			Case ok
				Hide
			Case cancel
				Hide
			Case findnext
				find=TextFieldText(findterm)
				host.activepanel.Invoke "FINDNEXT",find
			Case replaceit
				Replace=TextFieldText(replaceterm)
				If host.activepanel.Invoke("REPLACE",Replace)
					host.activepanel.Invoke "FINDNEXT",find
				EndIf				
			Case replaceall
				find=TextFieldText(findterm)
				Replace=TextFieldText(replaceterm)
				host.activepanel.Invoke "REPLACE",find+Chr(0)+Replace
		End Select
	End Method

	Function Create:TReplaceRequester(host:THost)
		Local	seek:TReplaceRequester
		seek=New TReplaceRequester
		seek.initrequester(host,"Find and Replace",380,200)
		CreateLabel("Find What",6,12,80,24,seek.window)
		seek.findterm=CreateTextField(88,8,180,20,seek.window)
		CreateLabel("Replace With",6,44,80,24,seek.window)
		seek.replaceterm=CreateTextField(88,40,180,20,seek.window)
		seek.findnext=CreateButton("Find Next",300,8,64,24,seek.window)
		seek.replaceit=CreateButton("Replace",300,40,64,24,seek.window)
		seek.replaceall=CreateButton("Replace All",300,72,64,24,seek.window)
		Return seek
	End Function
End Type
